package core

import (
	"huwhy.cn/go-spider/model"
)

type ResponseWorker struct {
	id     int
	state  bool
	spider *Spider
}

func (w *ResponseWorker) Run() {
	for w.state {
		resp := w.spider.resultQueue.Pop().(*model.Response)
		if resp.Err == nil {
			p := w.spider.getParser(resp.Request.Parser)
			if p != nil {
				result := p.Parse(resp)
				w.spider.storage.Store(result)
				if result.Requests != nil {
					for i := 0; i < len(result.Requests); i++ {
						w.spider.AddTask(result.Requests[i])
					}
				}
			} else {
				w.spider.storage.Store(resp.Data)
			}
		}
	}
}

func (w *ResponseWorker) Stop() {
	w.state = false
}
